<template>
  <div class="app-container">
    <el-transfer
      v-model="value"
      filterable
      :titles="['可选授权策略名称', '已选授权策略名称']"
      :button-texts="['移除策略', '授权策略']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      :data="data"
      @change="handleChange"
    >
      <div
        slot-scope="{ option }"
        class="item"
      >
        <div>
          <h2>{{ option.label }}</h2>
          <p>{{ option.description }}</p>
        </div>
        <span>{{ option.type }}</span>
      </div>
    </el-transfer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      value: []
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  created() {
    this.$route.meta.title = 'aa'
    this.getModel()
  },
  methods: {
    getModel() {
      const id = this.id
      this.$http.rbac.assignmentView(id).then(response => {
        const { data } = response
        this.data = data.items.available
        this.value = data.items.assigned
      })
    },
    handleChange(value, direction, movedKeys) {
      console.log('123')
      console.log(value, direction, movedKeys)
      if (direction === 'left') {
        this.$http.rbac
          .revoke(this.id, {
            items: movedKeys
          })
          .then(res => {
            console.log(res.data)
          })
      } else {
        this.$http.rbac
          .assign(this.id, {
            items: movedKeys
          })
          .then(res => {
            console.log(res.data)
          })
      }
    }
  }
}
</script>

<style lang="scss" scope>
.transfer-footer {
  margin-left: 20px;
  padding: 6px 5px;
}

.el-transfer-panel {
  width: 30%;
  &__body {
    height: 400px;
  }
  &__list.is-filterable {
    height: 100%;
  }
  &__item {
    .el-checkbox__input {
      display: none;
    }
  }
}

.el-transfer-panel__item.el-checkbox {
  margin: 0;
  padding: 10px;
  height: auto;
  line-height: initial;
  width: 100%;

  &::after {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 1px;
    border-bottom: 1px solid #f3f4f5;
    background: #f3f4f5;
    width: 100%;
    content: '';
  }
  .el-checkbox__label {
    line-height: initial;
    padding: 0;
    .item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      h2 {
        font-size: 14px;
        margin: 0;
      }
      p {
        font-size: 12px;
        margin: 0;
        font-weight: 300;
        overflow: hidden;
        color: #ccc;
      }
      span {
        align-self: flex-start;
        font-size: 12px;
        font-weight: 300;
      }
    }
  }
  &.is-checked {
    background: #1890ff;
    .el-checkbox__label {
      color: #fff;
      p {
        color: rgba(255, 255, 255, 0.5);
      }
    }
  }
}
</style>
